package com.xmg.p2p.base.service.impl;

import com.sun.tools.javac.util.CloseableURLClassLoader;
import com.xmg.p2p.base.domain.MailVerify;
import com.xmg.p2p.base.domain.Userinfo;
import com.xmg.p2p.base.mapper.MailVerifyMapper;
import com.xmg.p2p.base.mapper.UserinfoMapper;
import com.xmg.p2p.base.service.IUserinfoService;
import com.xmg.p2p.base.service.IVerifyCodeService;
import com.xmg.p2p.base.util.BidConst;
import com.xmg.p2p.base.util.BitStatesUtils;
import com.xmg.p2p.base.util.DateUtil;
import com.xmg.p2p.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StreamUtils;

import java.util.Date;
import java.util.UUID;

/**
 * Created by zsl on 2017/10/26.
 */
@Service
public class UserinfoServiceImpl implements IUserinfoService {

    @Value("${mail.hostUrl}")
    private String hostUrl;

    @Autowired
    private UserinfoMapper userinfoMapper;

    @Autowired
    private IVerifyCodeService verifyCodeService;

    @Autowired
    private MailVerifyMapper mailVerifyMapper;


    @Override
    public void update(Userinfo userinfo) {
        int ret = userinfoMapper.updateByPrimaryKey(userinfo);
        if (ret == 0) {
            throw new RuntimeException("乐观锁失败,Userinfo:" + userinfo.getId());
        }
    }

    @Override
    public void add(Userinfo ui) {
        userinfoMapper.insert(ui);
    }

    @Override
    public Userinfo get(Long id) {
        return userinfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public void bindPhone(String phoneNumber, String verifyCode) {
        Userinfo current = get(UserContext.getCurrent().getId());
        if (!current.getIsBindPhone()) {
            boolean ret = this.verifyCodeService.verify(phoneNumber, verifyCode);
            if (ret) {
                current.addState(BitStatesUtils.OP_BIND_PHONE);
                current.setPhoneNumber(phoneNumber);
                this.update(current);
            }
            else {
              throw new RuntimeException("绑定手机失败");
            }
        }
    }

    @Override
    public void sendVerifyEamil(String email) {
        Userinfo current=getCurrent();
        if(!current.getIsBindEmail()){
            String uuid= UUID.randomUUID().toString();
            StringBuilder content = new StringBuilder(100)
                    .append("点击<a href='").append(this.hostUrl)
                    .append("bindEmail.do?key=").append(uuid)
                    .append("'>这里</a>完成邮箱绑定,有效期为")
                    .append(BidConst.VERIFYEMAIL_VAILDATE_DAY).append("天");

            try {
                 System.out.println("发送邮件:" + email + "邮件内容:" + content);
//                mailService.sendMail(email, "邮箱验证邮件", content.toString());
                // 构造一个MailVerify对象;
                MailVerify mv = new MailVerify();
                mv.setEmail(email);
                mv.setSendDate(new Date());
                mv.setUserinfoId(current.getId());
                mv.setUuid(uuid);
                this.mailVerifyMapper.insert(mv);

            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("验证邮件发送失败!");
            }
        }


    }

    @Override
    public Userinfo getCurrent() {
        return get(UserContext.getCurrent().getId());
    }

    @Override
    public void bindEmail(String uuid) {
        MailVerify mv=this.mailVerifyMapper.selectByUUID(uuid);
        if(mv!=null){

        Userinfo current=get(mv.getUserinfoId());
        if(!current.getIsBindEmail()){
              if(mv!=null&& DateUtil.secondsBetween(mv.getSendDate(),new Date())<=BidConst.VERIFYEMAIL_VAILDATE_DAY*24*3600){
                  current.addState(BitStatesUtils.OP_BIND_EMAIL);
                  current.setEmail(mv.getEmail());
                  update(current);
                  return;
              }

          }
    }
        throw new RuntimeException("绑定邮箱失败!");
    }

    @Override
    public void updateBasicInfo(Userinfo userinfo) {
       Userinfo old=this.getCurrent();
       old.setEducationBackground(userinfo.getEducationBackground());
       old.setHouseCondition(userinfo.getHouseCondition());
       old.setIncomeGrade(userinfo.getIncomeGrade());
       old.setKidCount(userinfo.getKidCount());
       old.setMarriage(userinfo.getMarriage());
       if(!old.getIsBasicInfo()){
           old.addState(BitStatesUtils.OP_BASIC_INFO);
       }

       this.update(old);


    }


}
